package com.yitao.fdfs.controller;

import com.yitao.fdfs.FdfsApplication;
import com.yitao.fdfs.service.FdfsFileService;
import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Api(value = "/FastDFS",tags = {"FastDFS"},description = "FastDFS接口")
@RestController(value = "fastdfs接口测试")
public class FdfsFileController {

    private static Logger logger = LoggerFactory.getLogger(FdfsApplication.class);
    @Autowired
    private FdfsFileService fdfsFileService;

    /**
     * 文件上传
     *
     * @param file 文件
     * @return
     */
    @ApiOperation(value = "文件上传", notes = "文件上传，返回文件存储路径，不含映射根路径")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public DataOutput uploadImg(@ApiParam(value = "图片文件", required = true) @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        try {
            String path = fdfsFileService.uploadFile(file);
            return new DataOutput(CommonStatus.SUCCESS, path);
        } catch (Exception e) {
            logger.error("上传文件异常{}", e.getMessage());
            e.printStackTrace();
            return new DataOutput(CommonStatus.SERVER_ERROR, null);
        }
    }
}
